//
//  AppDelegate.swift
//  Monosaccharides
//
//  Created by iSolar on 2017/8/18.
//  Copyright © 2017年 nothing. All rights reserved.
//

import UIKit
import Alamofire

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,RCIMReceiveMessageDelegate {
    
    func onRCIMReceive(_ message: RCMessage!, left: Int32) {
        
    }
    
    var token: String!
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.setNavigationBar(index: 0)

        self.requestToken1()
        
        return true
    }

    func setNavigationBar(index: NSInteger) {
    
        window = UIWindow.init(frame: UIScreen.main.bounds)
        let tabbar = LMJTabBarController()
        tabbar.selectedIndex = index
        window?.rootViewController = tabbar
        window?.makeKeyAndVisible()
        
    }
    
//    func obtainRongCloudToken() {
//
//        let parameters = [
//            "userId": "15636970089",
//            "name": "你猜啊",
//            "portraitUri": "http:http://www.isolar88.com/upload/photo/3099/pc.jpg"
//        ]
//
//        // 请求头(时间,随机数,加密)
//        let Timestamp = String(format: "%.0f",NSDate().timeIntervalSince1970)
//        let Nonce: String = String(arc4random())
//        var sha1 = RONGAPPKEY + Nonce + Timestamp
//        // sha1()是写的一个分类里面的一个方法
//        sha1 = (sha1 as NSString).sha1()
//
//        // 网址
//        let urlstr = "https://api.cn.rong.io/user/getToken.json"
//        // 封装的AFN, 你也可以直接用AFN
//        let manage = AFHTTPSessionManager()
//
//        let request = NSMutableURLRequest()
//        request.timeoutInterval = 6
//
//        request.httpMethod = "POST"
//        // 键值对应, 前两个在融云注册应用时,官网获取的应用信息
//        // 我在上面定义过了, 此处没写
//        manage.requestSerializer.setValue(RONGAPPKEY, forHTTPHeaderField: "App-Key")
//        manage.requestSerializer.setValue(RONGSECRET, forHTTPHeaderField: "appSecret")
//        // 其他的几个请求头
//        manage.requestSerializer.setValue(Nonce, forHTTPHeaderField: "Nonce")
//        manage.requestSerializer.setValue(Timestamp, forHTTPHeaderField: "Timestamp")
//        manage.requestSerializer.setValue(sha1, forHTTPHeaderField: "Signature")
//        manage.requestSerializer.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
//
//        // POST发送请求
//        manage.post(urlstr, parameters: parameters, progress: { (progress) in
//
//        }, success: { (operation, data) in
//
//            print(data)
//
//            let token = data!["token"] as! String
//
//            print(token)
//
//        }) { (operation, error) in
//
//            print(error)
//
//        }
//
//    }

    func requestToken1() -> Void {
        let parameters = [
            "userId": "15636970089",
            "name": "你猜啊",
            "portraitUri": "http:http://www.isolar88.com/upload/photo/3099/pc.jpg"
        ] //请求token的用户信息
        let urlStr = "https://api.cn.ronghub.com/user/getToken.json" //网址接口
        let appKey = RONGAPPKEY
        let appSecret = RONGSECRET
        let nonce = "12345623"   //生成随机数
        let timestamp = "156983218484"//时间戳
        var sha1Value = appKey + nonce + timestamp
        sha1Value = sha1Value.sha1()//数据签名,sha1是一个加密的方法
        let headers = [ //照着文档要求写的Http 请求的 4个head
            "App-key":appKey
            ,"Nonce":nonce
            ,"Timestamp":timestamp
            ,"Signature":sha1Value
            ,"appSecret":appSecret
            ,"Content-Type":"application/x-www-form-urlencoded"
        ]
    
        Alamofire.request(urlStr, method: .post, parameters: parameters , encoding: URLEncoding.default, headers: headers).responseJSON { (response) in
            print(response)
            if let dic = response.result.value  as? NSDictionary{
                let code = dic.value(forKey: "code") as! NSNumber
                if code.stringValue == "200" {
                    
                    self.token = dic.value(forKey: "token") as! String
                    print(self.token)
                    if self.token != nil {
                        RCIM.shared().initWithAppKey(RONGAPPKEY)
                        RCIM.shared().receiveMessageDelegate = self
                        RCIM.shared().connect(withToken: self.token, success: { (result) in
                            
                        }, error: { (error) in
                            
                        }) {
                            
                        }
                    }
                }
            }
        }
    }
    
    
}

